package zsw.proxy.Cglib;

import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibHandler implements MethodInterceptor{
	
	  private Object target;//业务类对象，供代理方法中进行真正的业务方法调用
	  
	  CyclicBarrier b=new CyclicBarrier(23);
	  CountDownLatch c=new CountDownLatch(34);
	  
	  
	  public  Object getInstance(Object obj) {
		this.target=obj;
		Enhancer en=new Enhancer();
		en.setSuperclass(target.getClass());
		en.setCallback(this);
		return en.create();
		
	}

	@Override
	public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
		String methods=arg1.getName();
		int i=(int) (Math.random()*3);
		Long t1=System.currentTimeMillis();
		Thread.sleep(i*1000);
		Object invokeSuper = arg3.invokeSuper(arg0, arg2);
		Long t2=System.currentTimeMillis();
		System.out.println("方法："+methods+"调取完毕，耗时："+(t2-t1));
		return invokeSuper;
	}

}
